package com.media.publish;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.baselibrary.WikiConstants;
import com.baselibrary.activity.BaseActivity;
import com.media.publish.fragment.ViewImagerFragment;
import com.baselibrary.view.SpacesHorizontalDec;
import com.google.android.material.tabs.TabLayout;
import com.liaoinstan.springview.utils.DensityUtil;
import com.media.R;
import com.media.databinding.ActivityReleaseImagerBinding;

import java.util.ArrayList;

import com.media.publish.adapter.ReleasePreViewAdapter;


public class PreviewMediaActivity extends BaseActivity {

    private ActivityReleaseImagerBinding mBinding;
    private ArrayList<String> mUrls = new ArrayList<>();
    private ArrayList<Fragment> mFragments;
    private int mIndex;
    private ReleasePreViewAdapter mWikiIndicatorAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_release_imager;
    }

    @Override
    protected void init() {
        mBinding = getDataBinding();
        initView();
        initData();
        loaData();
        initListener();
    }

    @SuppressLint("ResourceType")
    private void initView() {
        mBinding.tlImager.setSelectedTabIndicatorHeight(0);
        mBinding.rvTableIndicator.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        mBinding.rvTableIndicator.addItemDecoration(new SpacesHorizontalDec(DensityUtil.dip2px(this, 5)));

    }

    private void initData() {

        mWikiIndicatorAdapter = new ReleasePreViewAdapter( getLayoutInflater());
        mBinding.rvTableIndicator.setAdapter(mWikiIndicatorAdapter);
        Intent mIntent = getIntent();
        mUrls = mIntent.getStringArrayListExtra(WikiConstants.WIKI_PICTURE_LIST);
        mIndex = mIntent.getIntExtra(WikiConstants.WIKI_PICTURE_INDEX, 0);
        if (mUrls == null)
            return;
        mFragments = new ArrayList<>();
        for (int i = 0; i < mUrls.size(); i++) {
            ViewImagerFragment mFragment = new ViewImagerFragment();
            setArguments(i, mFragment);
            mFragments.add(mFragment);
        }
        if (mUrls.size() > 1) {
            mBinding.tvTitleContent.setVisibility(View.VISIBLE);
            mBinding.tvTitleContent.setText(mIndex + "/" + mUrls.size());
        } else {
            mBinding.tvTitleContent.setVisibility(View.GONE);
        }
        mWikiIndicatorAdapter.onRefreshAdapter(mUrls);
    }

    private void loaData() {
        //适配器（容器都需要适配器）
        mBinding.vpImager.setOffscreenPageLimit(mFragments.size());//取消预加载
        mBinding.vpImager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            //选中的item
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                if (mFragments != null) {
                    return mFragments.size();
                } else {
                    return 0;
                }
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return "";
            }
        });
        //将TabLayout与Viewager绑定
        mBinding.tlImager.setupWithViewPager(mBinding.vpImager);
        mBinding.vpImager.setCurrentItem(mIndex, true);
    }

    private void initListener() {
        mBinding.ivTitleClose.setOnClickListener(view -> finish());
        mBinding.tlImager.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //选择时触发
                mWikiIndicatorAdapter.onRefreshAdapter(tab.getPosition());
                mBinding.tvTitleContent.setText((tab.getPosition() + 1) + "/" + mUrls.size());
                mBinding.rvTableIndicator.smoothScrollToPosition(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //未选择是触发
                mWikiIndicatorAdapter.onRefreshAdapter(tab.getPosition());
                mBinding.rvTableIndicator.smoothScrollToPosition(tab.getPosition());
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mBinding.vpImager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mBinding.tlImager));

        mBinding.vpImager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
                mBinding.tvTitleContent.setText((i + 1) + "/" + mUrls.size());
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        mWikiIndicatorAdapter.setOnItemClickLitener(new ReleasePreViewAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(int position, String url) {
                mBinding.vpImager.setCurrentItem(position, true);
            }
        });
    }

    private Bundle setArguments(int i, ViewImagerFragment mFragment) {
        String mUrl = mUrls.get(i);
        Bundle mBundle = new Bundle();
        int type;
        if (mUrl.contains("videotx") && (!mUrl.endsWith(".jpg")
                && !mUrl.endsWith(".png")
                && !mUrl.endsWith(".jpeg")
                && !mUrl.endsWith(".bmp"))) {
            //视频
            type = ViewImagerFragment.VIDEO_TYPE;
        } else {
            type = ViewImagerFragment.IMG_TYPE;

        }
        mBundle.putString("url", mUrls.get(i));
        mBundle.putInt(WikiConstants.WIKI_IMGPAGER_TYPE, type);
        mFragment.setArguments(mBundle);
        return mBundle;
    }


}
